package com.banciyuan.bcywebview.biz.main.newdiscover.ranklist;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.biz.main.discover.top.TopFragment;
import com.banciyuan.bcywebview.biz.main.discover.topcreator.TopCreatorActivity;
import com.banciyuan.bcywebview.biz.main.discover.topwork.NovelTopWorkActivity;
import com.banciyuan.bcywebview.biz.main.discover.topwork.TopWorkActivity;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import java.util.List;

import de.greenrobot.daoexample.model.RankItem;


public class RankListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int LINE_TYPE = 1;
    private static final int MAIN_TYPE = 2;
    private Context mContext;
    private List<RankItem> mRankItems;
    private LayoutInflater mLayoutInflater;
    private ImageLoader mImageLoader;

    public RankListAdapter(Context context, List<RankItem> rankItems) {
        mContext = context;
        mLayoutInflater = LayoutInflater.from(mContext);
        mRankItems = rankItems;
        mImageLoader = ImageLoader.getInstance();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == LINE_TYPE) {
            return new LineHolder(mLayoutInflater.inflate(R.layout.mainrankline_item, parent, false));
        }
        return new MainViewHolder(mLayoutInflater.inflate(R.layout.mainranklist_item, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        RankItem rankItem = mRankItems.get(position);
        if (getItemViewType(position) == LINE_TYPE) {
            LineHolder lineHolder = (LineHolder) holder;
            lineHolder.tvLineName.setText(rankItem.getName());
        } else {
            MainViewHolder mainViewHolder = (MainViewHolder) holder;
            if (null != rankItem.getInfo()) {
                for (int i = 0; i < rankItem.getInfo().size(); i++) {
                    final RankItem.RankInfo rankInfo = rankItem.getInfo().get(i);
                    mImageLoader.displayImage(rankInfo.getImg_src(), mainViewHolder.ivRankCovers[i], ImageOptions.options);
                    mainViewHolder.tvRankNames[i].setText(rankInfo.getName());
                    if ("top_works".equals(rankInfo.getCate()) && "writer".equals(rankInfo.getType()) && !TextUtils.isEmpty(rankInfo.getTitle())) {
                        mainViewHolder.viewRankWriteBgs[i].setVisibility(View.VISIBLE);
                        mainViewHolder.tvRankWriteTitles[i].setVisibility(View.VISIBLE);
                        mainViewHolder.tvRankWriteTitles[i].setText(Html.fromHtml(rankInfo.getTitle()));
                    } else {
                        mainViewHolder.viewRankWriteBgs[i].setVisibility(View.GONE);
                        mainViewHolder.tvRankWriteTitles[i].setVisibility(View.GONE);
                    }
                    mainViewHolder.viewItems[i].setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if ("top_insterests".equals(rankInfo.getCate())) {
                                ((RankListActivity) mContext).gotoIntersetRankActivity(rankInfo.getName());
                            } else if ("top_works".equals(rankInfo.getCate())) {
                                if ("drawer".equals(rankInfo.getType())) {
                                    gotoUtil.gotoAct(mContext, TopWorkActivity.class, TopWorkActivity.ILLUST_TYPE, rankInfo.getRank_type());
                                } else if ("writer".equals(rankInfo.getType())) {
                                    gotoUtil.gotoAct(mContext, NovelTopWorkActivity.class, TopWorkActivity.NOVEL_TYPE, rankInfo.getRank_type());
                                } else if ("coser".equals(rankInfo.getType())) {
                                    gotoUtil.gotoAct(mContext, TopWorkActivity.class, TopWorkActivity.class, rankInfo.getRank_type());
                                }
                            } else if ("top_users".equals(rankInfo.getCate())) {
                                if ("drawer".equals(rankInfo.getType())) {
                                    gotoUtil.gotoAct(mContext, TopCreatorActivity.class, TopFragment.ILLUST_TAG);
                                } else if ("writer".equals(rankInfo.getType())) {
                                    gotoUtil.gotoAct(mContext, TopCreatorActivity.class, TopFragment.WRITER_TAG);
                                } else if ("coser".equals(rankInfo.getType())) {
                                    gotoUtil.gotoAct(mContext, TopCreatorActivity.class, TopFragment.COS_TAG);
                                }
                            }
                        }
                    });
                }
            }
        }
    }

    private class MainViewHolder extends RecyclerView.ViewHolder {
        View viewItemOne;
        View viewItemTwo;
        View viewItemThree;
        View viewItemFour;
        View[] viewItems;

        ImageView[] ivRankCovers;

        TextView[] tvRankNames;

        View[] viewRankWriteBgs;
        TextView[] tvRankWriteTitles;

        MainViewHolder(View itemView) {
            super(itemView);
            viewItemOne = itemView.findViewById(R.id.view_rankitem_one);
            viewItemTwo = itemView.findViewById(R.id.view_rankitem_two);
            viewItemThree = itemView.findViewById(R.id.view_rankitem_three);
            viewItemFour = itemView.findViewById(R.id.view_rankitem_four);
            viewItems = new View[]{viewItemOne, viewItemTwo, viewItemThree, viewItemFour};

            ivRankCovers = new ImageView[]{(ImageView) viewItemOne.findViewById(R.id.siv_rank_pic),
                    (ImageView) viewItemTwo.findViewById(R.id.siv_rank_pic),
                    (ImageView) viewItemThree.findViewById(R.id.siv_rank_pic), (ImageView) viewItemFour.findViewById(R.id.siv_rank_pic)};

            tvRankNames = new TextView[]{(TextView) viewItemOne.findViewById(R.id.tv_rank_name),
                    (TextView) viewItemTwo.findViewById(R.id.tv_rank_name), (TextView) viewItemThree.findViewById(R.id.tv_rank_name),
                    (TextView) viewItemFour.findViewById(R.id.tv_rank_name)};


            viewRankWriteBgs = new View[]{viewItemOne.findViewById(R.id.view_rank_write_bg),
                    viewItemTwo.findViewById(R.id.view_rank_write_bg), viewItemThree.findViewById(R.id.view_rank_write_bg),
                    viewItemFour.findViewById(R.id.view_rank_write_bg)};
            tvRankWriteTitles = new TextView[]{(TextView) viewItemOne.findViewById(R.id.tv_rank_write_title),
                    (TextView) viewItemTwo.findViewById(R.id.tv_rank_write_title), (TextView) viewItemThree.findViewById(R.id.tv_rank_write_title),
                    (TextView) viewItemFour.findViewById(R.id.tv_rank_write_title),};
        }
    }

    private class LineHolder extends RecyclerView.ViewHolder {
        TextView tvLineName;

        LineHolder(View itemView) {
            super(itemView);
            tvLineName = (TextView) itemView.findViewById(R.id.tv_mainwork_time);
        }
    }

    @Override
    public int getItemCount() {
        return mRankItems.size();
    }

    @Override
    public int getItemViewType(int position) {
        if ("line_type".equals(mRankItems.get(position).getItemType())) {
            return LINE_TYPE;
        }
        return MAIN_TYPE;
    }
}
